package com.hgyc.mom.qms.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.qms.entity.QmsDefect;
import com.hgyc.mom.qms.vo.QmsDefectParam;
import com.hgyc.mom.qms.service.QmsDefectService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 常见缺陷Controller
 *
 * @author fwj
 * @since 2025-06-25
 */
@Tag(name = "常见缺陷控制器", description = "常见缺陷控制器")
@RestController
@RequestMapping("/qms/defect")
public class QmsDefectController {
    @Autowired
    private QmsDefectService qmsDefectService;

    @Operation(summary = "查询常见缺陷列表", description = "查询常见缺陷列表")
    @PostMapping("/pageList")
    public Result<IPage<QmsDefect>> pageList(@Parameter(description = "查询参数") @RequestBody QmsDefectParam qmsDefectParam) {
        Page<QmsDefect> page = new Page<>(qmsDefectParam.getPage(), qmsDefectParam.getPageSize());
        QmsDefect qmsDefect = new QmsDefect();
        BeanUtils.copyProperties(qmsDefectParam, qmsDefect);
        QueryWrapper<QmsDefect> wrapper = QueryWrapperUtil.build(qmsDefect);
        IPage<QmsDefect> list = qmsDefectService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取常见缺陷详细信息", description = "获取常见缺陷详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "常见缺陷ID") @PathVariable("id") Long id) {
        return Result.success(qmsDefectService.getById(id));
    }
    @Operation(summary = "新增常见缺陷", description = "新增常见缺陷")
    @PostMapping
    public Result<?> save(@Parameter(description = "常见缺陷") @RequestBody QmsDefect qmsDefect) {
        qmsDefectService.saveDefect(qmsDefect);
        return Result.success();
    }

    @Operation(summary = "修改常见缺陷", description = "修改常见缺陷")
    @PutMapping
    public Result<?> update(@Parameter(description = "常见缺陷") @RequestBody QmsDefect qmsDefect) {
        qmsDefectService.updateDefect(qmsDefect);
        return Result.success();
    }

    @Operation(summary = "删除常见缺陷", description = "删除常见缺陷")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(qmsDefectService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<QmsDefect> util = new ExcelUtil<>(QmsDefect.class);
        util.importTemplateExcel(response, "常见缺陷");
    }

    @Operation(summary = "导出常见缺陷列表", description = "导出常见缺陷列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsDefectParam qmsDefectParam) {
        QmsDefect qmsDefect = new QmsDefect();
        BeanUtils.copyProperties(qmsDefectParam, qmsDefect);
        QueryWrapper<QmsDefect> wrapper = QueryWrapperUtil.build(qmsDefect);
        List<QmsDefect> list = qmsDefectService.list(wrapper);
        ExcelUtil<QmsDefect> util = new ExcelUtil<>(QmsDefect.class);
        util.exportExcel(response, list, "常见缺陷数据");
    }

    @Operation(summary = "导入常见缺陷列表", description = "导入常见缺陷列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<QmsDefect> util = new ExcelUtil<>(QmsDefect.class);
        List<QmsDefect> qmsDefectList = util.importExcel(file.getInputStream());
        String message = qmsDefectService.importData(qmsDefectList, updateSupport);
        return Result.success(message);
    }
}